package com.crm.system.controller.finishOrderProductManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.common.util.ExcelUtil4;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.qo.finishOrderProductManager.AnewSendFinishProductQO;
import com.crm.model.qo.finishOrderProductManager.FinishOrderProductQO;
import com.crm.model.vo.finishOrderProduct.*;
import com.crm.model.vo.orderManager.ImportOrderNumber;
import com.crm.model.vo.returnMoney.ExportReturnMoneyVO;
import com.crm.model.vo.returnMoney.ImportReturnMoneyVO;
import com.crm.model.vo.systemManager.ContactImportVO;
import com.crm.system.service.cluePoolManager.ExcelUtil;
import com.crm.system.service.finishOrderProductManager.FinishOrderProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:chaifuyou
 * @Date: 2021/7/30 14:28
 */
@RestController
@RequestMapping("/finishOrderProduct")
@Slf4j
public class FinishOrderProductController {

    @Autowired
    FinishOrderProductService finishOrderProductService;

    @PostMapping("/selectFinishOrderProductList")
    private DataResponse selectFinishOrderProductList(HttpServletRequest request,
                                                      @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                      @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                      @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                      @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc
                                                      FinishOrderProductQO qo){
        DhPageInfo<FinishOrderProductVO> list = finishOrderProductService.selectFinishOrderProductList(pageIndex,pageSize,sortName,sortOrder,qo);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    @PostMapping("/updateFinishOrderProduct")
    private DataResponse updateFinishOrderProduct(@RequestBody FinishOrderProduct finishOrderProduct){
        finishOrderProductService.updateFinishOrderProduct(finishOrderProduct);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 导出opp客户购买产品
     * @param request
     * @param res
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/exportOppCustomerFinishProduct")
    public DataResponse exportOppCustomerFinishProduct(HttpServletRequest request, HttpServletResponse res,
                                                       @RequestParam MultipartFile file
    ) throws IOException {
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<ImportOppFinishProductVO> list = excelUtil.excelImport(file.getInputStream(), ImportOppFinishProductVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            List<ExportOppFinishProductVO> result = finishOrderProductService.exportOppCustomerFinishProduct(list);
            ExcelUtil4 excelUtil4 = new ExcelUtil4();
            String fileName = "OPP参会客户购买产品"+new Date().getTime() +".xlsx";
            excelUtil4.writeExcel(res,result,fileName,"sheet1", ExportOppFinishProductVO.class);

        } catch (Exception e) {
            log.error("contact importContact:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 导出数据
     * @param res
     * @returnexportFinishOrderProduct
     * @throws IOException
     */
    @GetMapping(value="/exportFinishOrderProduct")
    public void exportFinishOrderProduct( HttpServletResponse res,FinishOrderProductQO qo) throws IOException {
        List<ExportFinishOrderProductVO> list = finishOrderProductService.selectExportFinishOrderProduct(qo);
        ExcelUtil4 excelUtil4 = new ExcelUtil4();
        try {
            excelUtil4.writeExcel(res,list,"产品提成发放"+(new Date().getTime())+".xlsx","sheet1",ExportFinishOrderProductVO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入产品提成发放
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/importOrderProductRoyalty")
    public DataResponse importOrderProductRoyalty(HttpServletRequest request,@RequestParam MultipartFile file) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<ExportFinishOrderProductVO> list = excelUtil.excelImport(file.getInputStream(), ExportFinishOrderProductVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            jsonObject = finishOrderProductService.importOrderProductRoyalty(list);

        } catch (Exception e) {
            log.error("finishOrderProduct importOrderProductRoyalty:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }

    /**
     * 查询需要补发产品的信息
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @return
     */
    @PostMapping("/selectAnewSendFinishProduct")
    public DataResponse selectAnewSendFinishProduct(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                    @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                    @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                    @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc
                                                    AnewSendFinishProductQO qo){
        DhPageInfo<AnewSendFinishProductVO> list = finishOrderProductService.selectAnewSendFinishProduct(qo,pageIndex,pageSize,sortName,sortOrder);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 补发产品
     * @param orderId
     * @return
     */
    @PostMapping("/anewSendFinishProduct")
    public DataResponse anewSendFinishProduct(String orderId) throws ParseException {
        finishOrderProductService.anewSendFinishProduct(orderId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 查询订单下产品
     * @param pageIndex
     * @param pageSize
     * @param orderId
     * @return
     */
    @PostMapping("/selectFinishProduct")
    public DataResponse selectFinishProduct(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                            @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                            String orderId){
        DhPageInfo<FinishOrderProduct> list = finishOrderProductService.selectFinishProduct(orderId,pageIndex,pageSize);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 计算历史数据，业绩
     * @return
     */
    @PostMapping("/anewCountRoyalty")
    public DataResponse anewCountRoyalty(){
        finishOrderProductService.anewCountRoyalty();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    @PostMapping("/testFinish")
    public DataResponse testFinish(){
        finishOrderProductService.testFinish();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 同步finish表合同编号订单编号客户名称
     * @return
     */
    @PostMapping("/synOrderNumber")
    public DataResponse synOrderNumber(){
        finishOrderProductService.synOrderNumber();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 拆分历史合作合同产品
     * @return
     */
    @PostMapping("/partCooperationContractProduct")
    public DataResponse partCooperationContractProduct(){
        finishOrderProductService.partCooperationContractProduct();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 查询订单下的finish表数据（不含赠品）
     * @return
     */
    @PostMapping("/selectOrderFinishProduct")
    public DataResponse selectOrderFinishProduct(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                 @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                 String orderId){
        DhPageInfo<FinishOrderProductVO> list = finishOrderProductService.selectOrderFinishProduct(orderId,pageIndex,pageSize);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 查询余额支付的订单下的产品（不含赠品）
     * @return
     */
    @PostMapping("/selectBalanceOrderFinishProduct")
    public DataResponse selectBalanceOrderFinishProduct(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                        @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                        String customerId){
        DhPageInfo<FinishOrderProductVO> list = finishOrderProductService.selectBalanceOrderFinishProduct(customerId,pageIndex,pageSize);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 同步旧订单产品号
     * @return
     */
    @PostMapping("updateOldFinishProductNum")
    public DataResponse updateOldFinishProductNum(){
        finishOrderProductService.updateOldFinishProductNum();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    @PostMapping("selectFinishProductNum")
    public DataResponse selectFinishProductNum(HttpServletRequest request,
                                               @RequestParam MultipartFile file){
        ExcelUtil excelUtil = new ExcelUtil();
        List<String> list2= new ArrayList<>();
        try {
            List<ImportOrderNumber> list = excelUtil.excelImport(file.getInputStream(), ImportOrderNumber.class);//1.读取前端的excel文件并转换成list集合
            for (int i=0;i<list.size();i++){
                String orderNumber = list.get(i).getOrderNumber();
                finishOrderProductService.selectFinishProductNum(orderNumber);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

}
